﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "LicenceNoticeDialog.h" 
#include <GbmpNew.h>
#include <QCoreApplication>
#include <QDesktopServices>
#include <QDialog>
#include <QLabel>
#include <QSettings>
#include <QTextCodec>
#include <QUrl>
#include <QVBoxLayout>
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

namespace {
    static const QString s_folder("/QtLicense");
}

namespace Sample {
    GLDLicenceNoticeDialog::GLDLicenceNoticeDialog(const QString &qtVersion, QWidget *parent, const QString &language)
    #ifdef USE_GLD
        :GlodonFrame(parent)
    #else
        : QDialog(parent)
    #endif
    {
        //
        // Hide the WhatsThis button.
        setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

        QString exePath = QCoreApplication::applicationDirPath() + s_folder;
        QSettings settings(exePath + "/Qt_LGPLLicense.ini", QSettings::IniFormat, this);
        settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
        QString commonSectionName = "Common_" + language;

        QString titleText = settings.value(commonSectionName + "/Title", "About Qt").toString();
        setWindowTitle(titleText);
        QVBoxLayout *layout = NEW_AS_QT_LAYOUT(QVBoxLayout, this);
        QString noticeLabelText = settings.value(commonSectionName + "/LicenseText").toString();
        QLabel *noticeLabel = NEW_AS_QT(QLabel, noticeLabelText);
        noticeLabel->setWordWrap(true);
        layout->addWidget(noticeLabel);

        QString sourceText = settings.value(commonSectionName + "/SrcText").toString();
        QString urlKey("5.6.3/URL");
        if(!qtVersion.isEmpty() && settings.contains(qtVersion + "/URL"))
            urlKey = qtVersion + "/URL";
        sourceText = sourceText.arg(settings.value(urlKey).toString());

        QLabel *sourceLabel = NEW_AS_QT(QLabel, sourceText);
        connect(sourceLabel,SIGNAL(linkActivated(const QString &)),this,SLOT(openUrl(const QString &)));
        layout->addWidget(sourceLabel);

        QString gplLabelText = settings.value(commonSectionName + "/GPLText").toString().arg(exePath + "/LICENSE.GPLv3.txt");
        QLabel *GPLLabel = NEW_AS_QT(QLabel, gplLabelText);
        connect(GPLLabel,SIGNAL(linkActivated(const QString &)),this,SLOT(showGPL(const QString &)));
        layout->addWidget(GPLLabel);

        QString lgplLabelText = settings.value(commonSectionName + "/LGPLText").toString().arg(exePath + "/LICENSE.LGPLv3.txt");
        QLabel *LGPLLabel = NEW_AS_QT(QLabel, lgplLabelText);
        connect(LGPLLabel,SIGNAL(linkActivated(const QString &)),this,SLOT(showLGPL(const QString &)));
        layout->addWidget(LGPLLabel);

        setMinimumWidth(500);
    }

    GLDLicenceNoticeDialog::~GLDLicenceNoticeDialog()
    {

    }

    void GLDLicenceNoticeDialog::openUrl(const QString & url)
    {
        QDesktopServices::openUrl(QUrl(url));
    }

    void GLDLicenceNoticeDialog::showGPL(const QString & url)
    {
        QDesktopServices::openUrl(QUrl::fromLocalFile(url));
    }

    void GLDLicenceNoticeDialog::showLGPL(const QString & url)
    {
        QDesktopServices::openUrl(QUrl::fromLocalFile(url));
    }
}